import { Injectable } from '@nestjs/common';
import { DiaryChat, DiaryPolish } from './interfaces/diaryChat.interface';
import axios from 'axios';
import { InjectRepository } from '@nestjs/typeorm';
import { Diary } from 'src/diary/diary.entity';
import { Repository } from 'typeorm';
import { ConWrite, ConRead } from 'src/common/utils/conversation';
import { serviceInfoLog,serviceErrorLog } from "src/common/out/logger/access.logger";

@Injectable()
export class DiaryChatService {
  constructor(
    @InjectRepository(Diary)
    private diaryRepository: Repository<Diary>,
  ) { }

  charDiary(diary: DiaryChat) {
    let response = chatWithBot(diary);
    return response;
  }

  charDiaryAfter(diary: DiaryChat) {
    let response = chatWithBotAfter(diary);
    return response;
  }

  diaryPolish(diary: DiaryPolish) {
    // 根据diaryId获取日记内容
    let response = chatWithBotForPolish(diary);
    return response;
  }
}

async function chatWithBotAfter(chatDiary: DiaryChat) {
  return await axios.post('http://api.dify.dearaccount.com/v1/chat-messages', {
    inputs: {
      llmrole: '知心朋友',
      content: chatDiary.diary
    },
    query: chatDiary.content,
    response_mode: 'blocking',
    conversation_id: "",
    user: chatDiary.userid,
  }, {
    headers: {
      Authorization: 'Bearer app-ErDhGPZvyqR1vIiRv7dUhdAp',
      'Content-Type': 'application/json'
    }
  }).then(response => {
    serviceInfoLog(response.data.answer);
    return response.data.answer;
  }).catch(error => {
    serviceErrorLog(error);
    return "";
  });
}


async function chatWithBot(chatDiary: DiaryChat) {
  // 获取对话id
  let conversation_id = await ConRead(chatDiary.userid + "/" + chatDiary.diaryId);
  return await axios.post('http://api.dify.dearaccount.com/v1/chat-messages', {
    inputs: {
      llmrole: '知心朋友',
      content: chatDiary.diary
    },
    query: chatDiary.content,
    response_mode: 'blocking',
    conversation_id: conversation_id,
    user: chatDiary.userid,
  }, {
    headers: {
      Authorization: 'Bearer app-ErDhGPZvyqR1vIiRv7dUhdAp',
      'Content-Type': 'application/json'
    }
  }).then(response => {
    serviceInfoLog(response.data.answer);
    if (response.data && response.data.conversation_id) {
      ConWrite(chatDiary.userid + "/" + chatDiary.diaryId, response.data.conversation_id, 24 * 60 * 60);
    }
    return response.data.answer;
  }).catch(error => {
    serviceErrorLog(error);
    return "";
  });
}

async function chatWithBotForPolish(chatDiary: DiaryPolish) {
  return await axios.post('http://api.dify.dearaccount.com/v1/completion-messages', {
    inputs: {
      content: chatDiary.diary
    },
    //  query: '帮我美化下这个内容',
    response_mode: 'blocking',
    conversation_id: chatDiary.diaryId + "/" + chatDiary.userid,
    user: chatDiary.userid,
  }, {
    headers: {
      //  Authorization: 'Bearer app-RV44Vg5ZpeuBsY4IbhhCh336', // prompt润色
      Authorization: 'Bearer app-SZNr2JzLWA7hqn0egVciMvuc', // agent润色
      'Content-Type': 'application/json'
    }
  }).then(response => {
    serviceInfoLog(response.data.answer);
    return response.data.answer;
  }).catch(error => {
    serviceErrorLog(error)
    return "";
  });
}